package org.jiangnantaste.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainMenuActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("江南味道");
        setContentView(R.layout.main_menu);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));
    }
    
    public class ImageAdapter extends BaseAdapter {
        private Context mContext;
        public ImageAdapter(Context c) {
            mContext = c;
        }
        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            ImageView imageView;
            if (convertView == null) {

                imageView = new ImageView(mContext);

                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(mThumbIds[position]);

            switch (position) {
			case 0:
				imageView.setOnClickListener(listAllDeliveryOrderStatusBoTabListener);
				break;
			case 1:
				imageView.setOnClickListener(listAllRestaurantsListener);
				break;
			case 2:
				imageView.setOnClickListener(listAllImagesListener);
				break;
			case 3:
				imageView.setOnClickListener(listAllMenuItemsListener);
				break;
			case 4:

				imageView.setOnClickListener(exitListener);
				break;
			default:
				break;
			}
            
            return imageView;
        }

        private Integer[] mThumbIds = {
                R.drawable.btn_mydeliveryorder,R.drawable.btn_categorieddeliveryorder,R.drawable.btn_categorieddeliveryorder, R.drawable.btn_deliveryorder,R.drawable.zhuxiao
        };
    }
	
	OnClickListener listAllDeliveryOrderStatusBoTabListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(MainMenuActivity.this, ListAllDeliveryOrderTabActivity.class);
			startActivity(intent);
		}
	};
	
	OnClickListener listAllRestaurantsListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(MainMenuActivity.this, ListAllRestaurantsActivity.class);
			startActivity(intent);
		}
	};
	
	OnClickListener listAllImagesListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(MainMenuActivity.this, ListAllImagesActivity.class);
			startActivity(intent);
		}
	};
	
	OnClickListener listAllMenuItemsListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.setClass(MainMenuActivity.this, ListAllMenuItemsActivity.class);
			startActivity(intent);
		}
	};

    OnClickListener exitListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			logout();
		}
	};
	
	private void logout(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("")
		       .setCancelable(false)
		       .setPositiveButton("", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	  SharedPreferences pres = getSharedPreferences("user_msg", MODE_WORLD_WRITEABLE);
		        	  SharedPreferences.Editor editor = pres.edit();
		        	  editor.putString("userid", "");
		        	  editor.putString("username", "");
		        	  Intent intent = new Intent();
		        	  intent.setClass(MainMenuActivity.this, LoginActivity.class);
		        	  startActivity(intent);
		           }
		       })
		       .setNegativeButton("", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
	}
}